<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 从以下学员名单中随机选出4个学员：
      // var arr = ["鹿晗","王俊凯","蔡徐坤","彭于晏","周杰伦","刘德华","赵本山"]；
      // 注意：不要有重复的学员

      var arr = [
        "鹿晗",
        "王俊凯",
        "蔡徐坤",
        "彭于晏",
        "周杰伦",
        "刘德华",
        "赵本山",
      ];
      function getRandom(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min);
      }

      //   var obj = {};
      //   var newArr = [];
      //   for (var i = 0; i < 4; i++) {
      //     var num = getRandom(0, arr.length - 1);
      //     if (obj[arr[num]]) {
      //       break;
      //     } else {
      //       obj[arr[num]] = 1;
      //     }
      //     console.log(arr[num]);
      //   }

      var newArr = [];
      for (; newArr.length < 4; ) {
        var index = getRandom(0, arr.length - 1);
        var num = arr[index];
        if (newArr.indexOf(num) === -1) {
          newArr.push(num);
        }
      }
      console.log(newArr);
    </script>
  </body>
</html>
